package org.attached.user.service.impl;

import java.util.Date;

import org.attached.user.constants.UserConstants;
import org.attached.user.entity.ChatUser;
import org.attached.user.mapper.ChatUserMapper;
import org.attached.user.service.ChatUserService;
import org.attached.user.service.RedisService;
import org.common.utils.CommonUtils;
import org.common.utils.JSONUtils;
import org.common.utils.TokenUtils;
import org.common.vo.ServiceException;
import org.common.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Mr.古
 * @since 2018-05-05
 */
@Service
public class ChatUserServiceImpl extends ServiceImpl<ChatUserMapper, ChatUser> implements ChatUserService {

	@Autowired
	private ChatUserMapper chatUserMapper;
	@Autowired
	private RedisService redisService;

	@Override
	public ChatUser getUserDetail(String userName) {
		return null;
	}
	
	@Override
	@Transactional
	public ChatUser register(ChatUser chatUser) {
		if (chatUser == null || CommonUtils.isEmpty(chatUser.getUserName())) {
			throw new ServiceException("用户信息不完整！");
		}
		chatUser.setIsOnLine(UserConstants.USER_NOT_ONLINE);
		chatUser.setCreateTime(new Date());
		chatUserMapper.insert(chatUser);
		redisService.setValueOperations(chatUser.getUserName(), JSONUtils.beanToJson(chatUser));
		return chatUser;
	}
	
	@Override
	public ChatUser login(String token) {
		UserVo userVo = TokenUtils.analysisToken(token);
		return null;
	}
}
